package com.objecteffects.template.web.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.objecteffects.template.web.utils.NotLikedPost;

import org.scribe.model.Token;

import net.sourceforge.stripes.action.ActionBeanContext;

/**
 * @author Rusty Wright
 */
public class TemplateActionBeanContext extends ActionBeanContext {
    private static final String REQUEST_TOKEN = "request_token";
    private static final String TUMBLR_URL = "tumblr_url";
    private static final String NOT_LIKED_POSTS = "not_liked_posts";
    private static final String STARTING_OFFSET = "starting_offset";
    private static final String BATCH_SIZE = "batch_size";
    private static final String USER_ID = "user_id";

    public Token getRequestToken() {
        return (Token) this.getRequest().getSession()
            .getAttribute(TemplateActionBeanContext.REQUEST_TOKEN);
    }

    public void setRequestToken(final Token requestToken) {
        this.getRequest()
        .getSession()
        .setAttribute(TemplateActionBeanContext.REQUEST_TOKEN, requestToken);
    }

    public String getTumblrUrl() {
        return (String) this.getRequest().getSession()
            .getAttribute(TemplateActionBeanContext.TUMBLR_URL);
    }

    public void setTumblrUrl(final String tumblr) {
        this.getRequest().getSession()
        .setAttribute(TemplateActionBeanContext.TUMBLR_URL, tumblr);
    }

    @SuppressWarnings("unchecked")
    public List<NotLikedPost> getNotLikedPosts() {
        return (List<NotLikedPost>) this.getRequest().getSession()
            .getAttribute(TemplateActionBeanContext.NOT_LIKED_POSTS);
    }

    public void setNotLikedPosts(final List<NotLikedPost> notLikedPosts) {
        this.getRequest()
            .getSession()
        .setAttribute(TemplateActionBeanContext.NOT_LIKED_POSTS,
                          notLikedPosts);
    }

    public Integer getStartingOffset() {
        return (Integer) this.getRequest().getSession()
            .getAttribute(TemplateActionBeanContext.STARTING_OFFSET);
    }

    public void setStartingOffset(final Integer startingOffset) {
        this.getRequest()
            .getSession()
        .setAttribute(TemplateActionBeanContext.STARTING_OFFSET,
                          startingOffset);
    }

    public Integer getBatchSize() {
        return (Integer) this.getRequest().getSession()
            .getAttribute(TemplateActionBeanContext.BATCH_SIZE);
    }

    public void setBatchSize(final Integer batchSize) {
        this.getRequest().getSession()
        .setAttribute(TemplateActionBeanContext.BATCH_SIZE, batchSize);
    }

    public Long getUserId() {
        return (Long) this.getRequest().getSession()
            .getAttribute(TemplateActionBeanContext.USER_ID);
    }

    public void setUserId(final Long userEntityId) {
        this.getRequest().getSession()
        .setAttribute(TemplateActionBeanContext.USER_ID, userEntityId);
    }

    public void logout() {
        this.setUserId(null);

        final HttpSession session = this.getRequest().getSession();

        if (session != null) {
            session.invalidate();
        }
    }
}
